/*
 * JSFConsole - Lightweight JSF Application Monitor
 * Copyright (C) 2009  Grzegorz Bugaj
 * http://www.gregbugaj.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.gregbugaj.jsfconsole.agent.tracker;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * This class represents all tracking information associated with single request
 * @author devil
 *
 */
public class RequestInfo {
	private Date requestTime;
	private List<EventInfo> events;
	private int facesId;
	private String requestPath;


	public RequestInfo(String requestPath){
		events=new ArrayList<EventInfo>();
		requestTime = new Date();
		this.requestPath=requestPath;
	}


	public String getRequestPath() {
		return requestPath;
	}


	public Date getRequestTime() {
		return requestTime;
	}

	public void setRequestTime(Date requestTime) {
		this.requestTime = requestTime;
	}

	public int getFacesId() {
		return facesId;
	}

	public void setFacesId(int facesId) {
		this.facesId = facesId;
	}

	public List<EventInfo> getEvents() {
		return events;
	}

	public void setEvents(List<EventInfo> events) {
		this.events = events;
	}

	public void addEventInfo(EventInfo info){
		events.add(info);
	}

	@Override
	public String toString() {
		String out="";
		out=getClass().getSimpleName()+" "+requestTime +" : "+requestPath;
		return out;
	}

	/**
	 * Default comparator to compare 2 RequestInfo instances
	 * @return Comparator that sorts in descending order (Latest on top)
	 */
	public static Comparator<? super RequestInfo> getComparator() {
		return (new Comparator<RequestInfo>(){
			
			public int compare(RequestInfo o1, RequestInfo o2) {
				int result=0;
				if(o1.getRequestTime().getTime()>o2.getRequestTime().getTime()){
					result=-1;
				}else{
					result=1;
				}
				return result;
			}
		});
	}
}
